<?php

/* Vanilla Journal
 * class.vj.catview.php
 * Weizhong Yang
 */

function inCatArray($item=0, $array) {
   foreach($array as $a){
      if($item == $a->cat_id){
         return 1;
      }
   }
   return 0;
}

/* The CatView Class */

class catview {
   var $err_msg = "";
   var $msg = "";
   var $catview_cat=0;
   var $catview_year=0;
   var $catview_posts=array();
   var $catview_years=array();
   var $catview_newstyear=0;
   var $catview_oldestyear=0;
   var $perpage=12;
   var $page_count=1;
   var $current_page=0;
   var $previous_vol = 0;
	
   function catview($id=0, $year=0, $current_page=1){
      global $vjdb;
      global $vj;

      $this->current_page = $current_page;
      if($this->current_page < 1) {
         $this->current_page = 1;
      }
      $vj->thispage = "categories";
      if(!$id){
	 $this->err_msg = "您沒有指定單元！請指定您要瀏覽的文章單元！";
	 return;
      }
      $this->catview_cat = $id;
      if(!inCatArray($this->catview_cat, $vj->cats)) {
	 $this->err_msg = "系統中沒有您所指定瀏覽的單元！";		
	 return;
      }

      if(is_logined()){ $volume = $vj->query_newst_volume();}
      else { $volume = $vj->published_newst(); }
      $query = "SELECT CREATE_DATE FROM ".$vjdb->volumes." WHERE ID=".$volume." ORDER BY CREATE_DATE LIMIT 1";
      $datestring = $vjdb->get_var($query);
      $date = strtotime($datestring);
      $this->catview_newstyear = date("Y", $date);

      $this->catview_year = $year;
      if($this->catview_year && !is_numeric($this->catview_year)) {
	 $this->err_msg = "您輸入的年份不正確！";		
	 return;
      }
      if(!$this->catview_year) {
	 if(!$volume){
	    $this->err_msg = "系統中目前還沒有任何一期期刊！";
	    return;
	 } else {
	    $this->catview_year = $this->catview_newstyear;
	 }
      }
      $start = $this->catview_year."-1-1";
      $end = $this->catview_year."-12-31";
      $query = "SELECT ID, ALIAS, ALIAS_EXT, TOPIC FROM ".$vjdb->volumes." WHERE CREATE_DATE > '".$start."' AND CREATE_DATE < '".$end."' ORDER BY CREATE_DATE ASC";
      $results = $vjdb->get_results($query, ARRAY_A);
      if($results) {
	 $i = 0;
	 foreach($results as $result) {
	    $vj->volumes[$i] = $result;
	    $i++;
	 }
      }
      if(count($vj->volumes) == 0) {
	 $this->msg = "在 ".$this->catview_year." 一整年裡，都沒有發行期刊！";
	 return;
      }
      $this->load_posts();
      if(count($this->catview_posts) == 0 ) {
	 $this->msg = "在 ".$this->catview_year." 一整年裡，都沒有「".cat_name($this->catview_cat)."」單元的文章！";
	 return;
      }
      $this->get_years();
   }

   function error(){
      if($this->err_msg) {
	 vj_die("<h2>".$this->err_msg."</h2>", "載入時發生錯誤！");
      }
   }

   function msg(){
      if($this->msg) {
	 echo("<h2>".$this->msg."</h2>");
	 echo("<p>請試試看其他年份或是其他單元。</p>");
      }
   }

   function get_years() {
      global $vjdb;
      $query = "SELECT CREATE_DATE FROM ".$vjdb->volumes." ORDER BY CREATE_DATE ASC LIMIT 1";
      $datestring = $vjdb->get_var($query);
      $date = strtotime($datestring);
      $this->catview_oldestyear = date("Y", $date);
      for($i = $this->catview_oldestyear; $i <= $this->catview_newstyear; $i++) {
	 $start = $i."-1-1";
	 $end = $i."-12-31";
	 $query = "SELECT COUNT(ID) FROM ".$vjdb->volumes." WHERE CREATE_DATE > '".$start."' AND CREATE_DATE < '".$end."'";
	 if($vjdb->get_var($query)) {
	    $this->catview_years[] = $i;
	 }
      }
   }

   function title($prefix="", $subfix="") {
      echo $prefix.$this->catview_year."年「".cat_name($this->catview_cat)."」單元的所有文章如下".$subfix;
   }

   function load_posts() {
      global $vjdb;
      global $vj;
      $this->catview_posts = array();
      $all = array();
      foreach ($vj->volumes as $v) {
	 $query= "SELECT ID, TOPIC, AUTHOR, DISPLAY, CAT, IMPORTANCE FROM ".$vjdb->post." WHERE VOLUME = '".$v['ID']."' AND CAT = '$this->catview_cat' ORDER BY POST_ORDER ASC;";
	 $results = $vjdb->get_results($query, ARRAY_A);
	 $all = array_merge((array)$all, (array)$results);
      }
      $count = count($all);
      $this->page_count = (int)($count / $this->perpage) + 1;
      if($this->current_page > $this->page_count) {
	 $this->current_page = $this->page_count;
      }
      $begin = ($this->current_page-1) * $this->perpage;
      if($begin < 0) {
         $begin = 0;
      }
      $end = ($this->current_page) * $this->perpage;
      for($i = $begin; $i < $end; $i++) {
         if($i > count($all)) {
	    continue;
	 }
	 if($all[$i]) $this->catview_posts[] = $all[$i];
      }
   }

   function show_year_links() {
      if(!$this->catview_years) {
	 return;
      }
      echo "<ul>\n";
      foreach($this->catview_years as $year) {
	 echo "<li><a title=\"".$year."\" href=\"".vjinfo('url')."index.php?cat=".$this->catview_cat."&year=".$year."\">".$year."</a></li>\n";
      }
      echo "</ul>\n";
   }

   function show_cat_links() {
      global $vj;
      $cats = $vj->cats;
      if(!$cats) return;
      echo "<ul>\n";
      foreach($cats as $cat) {
         echo "<li><a title=\"".$cat->cat_desc."\" href=\"".vjinfo('url')."index.php?cat=".$cat->cat_id."&year=".$this->catview_year."\">".$cat->cat_name."</a></li>\n";
      }
      echo "</ul>\n";
   }

   function show_page_links() {
      $str .= "<p>第 ";
      for($i = 1; $i <= $this->page_count; $i++) {
         if($i == $this->current_page) {
	    $str .= $i."&nbsp;";
	 } else {
	    $str .= "<a title=\"".$catview_year."\" href=\"".vjinfo('url')."index.php?cat=".$this->catview_cat."&year=".$catview_year."&page=".$i."\">".$i."</a>&nbsp\n";
	 } 
      }
      $str .= " 頁</p>";
      echo $str;
   }   
}

/* Functions */

function catview_year_links() {
   global $catview;
   $catview->show_year_links();
}

function catview_cat_links() {
   global $catview;
   $catview->show_cat_links();
}

function catview_page_links() {
   global $catview;
   $catview->show_page_links();
}

?>
